package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultCategory;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultOther.SearchResultOtherItems;
import com.da.lon.wang.xlg.util.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class Classify2Adapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<SearchResultOtherItems> searchResultOtherItems;
    private List<SearchResultCategory> searchResultCategories;
    private Context context;
    private String styleString;

    public Classify2Adapter(List<SearchResultOtherItems> list,
                            List<SearchResultCategory> list2, Context context, String style) {
        // TODO Auto-generated constructor stub

        inflater = LayoutInflater.from(context);

        this.context = context;

        this.styleString = style;

        if (list == null) {
            searchResultOtherItems = new ArrayList<SearchResultOtherItems>();
        } else {
            this.searchResultOtherItems = list;
        }

        if (list2 == null) {
            searchResultCategories = new ArrayList<SearchResultCategory>();
        } else {
            this.searchResultCategories = list2;
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (null != styleString && ("1").equals(styleString)) {
            return searchResultCategories != null ? searchResultCategories
                    .size() : 0;
        } else {
            return searchResultOtherItems != null ? searchResultOtherItems
                    .size() : 0;
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub

        if (styleString.equals("1")) {
            return searchResultCategories.get(position);
        } else {
            return searchResultOtherItems.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        try {
            convertView = inflater.inflate(R.layout.ouka_classify2_item, null);

            TextView nameTextView = ViewHolder.get(convertView,
                    R.id.ouka_classify2_itname);

            if (styleString.equals("1")) {

                SearchResultCategory searchResultCategory = searchResultCategories
                        .get(position);

                nameTextView.setText(searchResultCategory.getName());

            } else {

                SearchResultOtherItems searchResultOtherItem = searchResultOtherItems
                        .get(position);

                nameTextView.setText(searchResultOtherItem.getId());

            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return convertView;

    }

}
